package net.example.model.contract;

import com.oraycn.omcs.utils.BufferUtils;

import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class ResetPasswordContract {

    public ResetPasswordContract()
    { }

    /**
     * 重置密码协议
     * */
    public ResetPasswordContract(String phone, String smsCode, String newPasswordMD5)
    {
        this.Phone = phone;
        this.SmsCode = smsCode;
        this.NewPasswordMD5 = newPasswordMD5;
    }

    public String Phone ;
    public String SmsCode ;
    public String NewPasswordMD5 ;

    public byte[] serialize() throws Exception {
        byte[] bPhone = Phone.getBytes(StandardCharsets.UTF_8);
        byte[] bSmsCode = SmsCode.getBytes(StandardCharsets.UTF_8);
        byte[] bNewPasswordMD5 = NewPasswordMD5.getBytes(StandardCharsets.UTF_8);
        int contractLen = 4 + 4 + bPhone.length + 4 +bSmsCode.length+4+bNewPasswordMD5.length;
        ByteBuf body = BufferUtils.newBuffer();
        body.writeInt(contractLen);
        body.writeInt(bNewPasswordMD5.length);
        body.writeBytes(bNewPasswordMD5);
        body.writeInt(bPhone.length);
        body.writeBytes(bPhone);
        body.writeInt(bSmsCode.length);
        body.writeBytes(bSmsCode);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(), 0, result, 0, result.length);
        return result;
    }
}
